package geek;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author sam
 * 2021/10/19 22:18
 * @description
 * @editor
 * @description 判断括号是否匹配
 * ()[]
 */
public class Lesson08 {

    @Test
    public  void bracketsTest() {
        assertTrue(brackets("({})[]"));
        assertFalse(brackets("(({})[]"));
        assertTrue(brackets("[({})][]"));
        assertTrue(brackets("[({})][]"));
    }

    public  boolean brackets(String param) {
        Map<String, String> map = new HashMap<>();
        map.put(")", "(");
        map.put("]", "[");
        map.put("}", "{");
        char[] chars = param.toCharArray();
        Stack<String> stringStack = new Stack<>();
        for (char aChar : chars) {
            if (!map.containsKey(String.valueOf(aChar))) {
                stringStack.push(String.valueOf(aChar));
            } else {
                if (map.get(String.valueOf(aChar)).equals(stringStack.peek())) {
                    stringStack.pop();
                }
            }
        }
        return stringStack.isEmpty();
    }

}
